package com.sec.bnjey.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class Utils {

	/** �� �ð� */
	static public String getDate() {
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
		return formatter.format(date);
	}

	static public String getYearFormDate(long date) {
		Date d = new Date(date);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		return formatter.format(d);
	}

	static public String getTimeFormDate(long date) {
		Date d = new Date(date);
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		return formatter.format(d);
	}

	static public String getDirectorySize(File file) {
		if (file.isDirectory() == false)
			return null;

		File[] flist = file.listFiles();
		long fsize = 0;
		for (int i = 0; i < flist.length; i++) {
			fsize = fsize + flist[i].length();
		}
		return formatSize(fsize);
	}

	static final int ERROR = -1;

	static public boolean externalMemoryAvailable() {
		return android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
	}

	static public long getAvailableInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
	}

	static public long getTotalInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		return totalBlocks * blockSize;
	}

	static public long getAvailableExternalMemorySize() {
		if (externalMemoryAvailable()) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			return availableBlocks * blockSize;
		} else {
			return ERROR;
		}
	}

	static public long getTotalExternalMemorySize() {
		if (externalMemoryAvailable()) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long totalBlocks = stat.getBlockCount();
			return totalBlocks * blockSize;
		} else {
			return ERROR;
		}
	}

	static public long getMemorySize(String path) {
		StatFs stat = new StatFs(path);
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
	}

	static public String formatSize(long size) {
		String suffix = null;

		if (size < 0)
			return "-";
		if (size >= 1024) {
			suffix = "KB";
			size /= 1024;
			if (size >= 1024) {
				suffix = "MB";
				size /= 1024;
				if (size >= 1024) {
					suffix = "GB";
					size /= 1024;
				}
			}
		}

		StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

		int commaOffset = resultBuffer.length() - 3;
		while (commaOffset > 0) {
			resultBuffer.insert(commaOffset, ',');
			commaOffset -= 3;
		}

		if (suffix != null)
			resultBuffer.append(suffix);
		return resultBuffer.toString();
	}

	static public String formatFloatValue(float value) {
		StringBuilder resultBuffer = new StringBuilder(Float.toString(value));
		int dotOffset = 0;
		if (resultBuffer.toString().contains(".") == true) {
			dotOffset = resultBuffer.length()
					- resultBuffer.toString().indexOf(".");
		}

		int commaOffset = resultBuffer.length() - 3 - dotOffset;
		while (commaOffset > 0) {
			resultBuffer.insert(commaOffset, ',');
			commaOffset -= 3;
		}
		return resultBuffer.toString();
	}

	static public String formatIntValue(int value) {
		StringBuilder resultBuffer = new StringBuilder(Integer.toString(value));
		int dotOffset = 0;
		int commaOffset = resultBuffer.length() - 3 - dotOffset;
		while (commaOffset > 0) {
			resultBuffer.insert(commaOffset, ',');
			commaOffset -= 3;
		}
		return resultBuffer.toString();
	}

}
